package ink.lovejinhu.leetcode;

import java.util.Stack;

/**
 * @author jinhu
 * created 2021-07-08 11:29
 */
public class Solution_0232 {
    Stack<Integer> v1;
    Stack<Integer> v2;
    /**
     * 用栈实现队列
     * 栈先进后出
     * 队列先进先出
     */
    /**
     * Initialize your data structure here.
     */
    public Solution_0232() {   //初始化栈
        v1 = new Stack<Integer>();
        v2 = new Stack<Integer>();
    }

    /**
     * Push element x to the back of queue.
     */
    public void push(int x) {
        if (v1.isEmpty()) {   //v1是空
            v2.push(x);
        } else {
            v1.push(x);
        }
    }

    /**
     * Removes the element from in front of queue and returns that element.
     */
    public int pop() {
        if (v1.isEmpty()) {
            int returnValue = Integer.MIN_VALUE;
            while (!v2.isEmpty()) { //非空
                returnValue = v2.pop();
                if (!v2.isEmpty()){
                    v1.push(returnValue);
                }
            }
            while (!v1.isEmpty()){
                v2.push(v1.pop());
            }
            return returnValue;
        }else {
            int returnValue = Integer.MIN_VALUE;
            while (!v1.isEmpty()) { //非空
                returnValue = v1.pop();
                if (!v1.isEmpty()){
                    v2.push(returnValue);
                }
            }
            while (!v2.isEmpty()){
                v1.push(v2.pop());
            }
            return returnValue;
        }

    }

    /**
     * Get the front element.
     */
    public int peek() {
        if (v1.isEmpty()) {
            int returnValue = Integer.MIN_VALUE;
            while (!v2.isEmpty()) { //非空
                returnValue = v2.pop();

                    v1.push(returnValue);
                }
            while (!v1.isEmpty()){
                v2.push(v1.pop());
            }
            return returnValue;
        }else {
            int returnValue = Integer.MIN_VALUE;
            while (!v1.isEmpty()) { //非空
                returnValue = v1.pop();

                    v2.push(returnValue);

            }
            while (!v2.isEmpty()){
                v1.push(v2.pop());
            }
            return returnValue;
        }
    }

    /**
     * Returns whether the queue is empty.
     */
    public boolean empty() {
        if (v1.isEmpty() && v2.isEmpty())
            return true;
        else
            return false;
    }

    public static void main(String[] args) {
        Stack<Integer> integers = new Stack<>();
      //  System.out.println(integers.pop());
        Solution_0232 solution_0232 = new Solution_0232();
        solution_0232.push(1);
        solution_0232.push(2);
        solution_0232.push(3);
        solution_0232.push(4);
        for (int i = 0; i < 4; i++) {
            int pop = solution_0232.pop();
            System.out.println(pop);
        }

    }
}
